package zc.cennavi.taxi.util;

import java.util.Iterator;
import java.util.Map;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.util.Log;

public class SettingSharePreference {
	public static final String CITY_SHANGHAI = "CITY_SHANGHAI";
	public static final String CITY_BEIJING = "CITY_BEIJING";
	private final String CURRENT_CITY = "CURRENT_CITY";
	private final String APPLICATION_FIRST = "APPLICATION_FIRST";
	private final String MAPVIEW_FIRST = "MAPVIEW_FIRST";
	private SharedPreferences prefs = null;
	private Context mContext = null;

	public SettingSharePreference(Context context) {
		super();
		this.mContext = context;
	}

	private void intSharedPreferences(Context context) {
		if (prefs == null) {
			prefs = context.getSharedPreferences("SETTING.xml", Context.MODE_PRIVATE);
		}
	}

	public boolean isShangHai() {
		intSharedPreferences(mContext);
		String temp = prefs.getString(CURRENT_CITY, "--");
		Log.i("test",temp);
		Log.i("test","isShangHai");
		boolean flag = false;
		if ("--".equals(temp)) {
			checkBeijing();
		}

		temp = prefs.getString(CURRENT_CITY, "--");
		Log.i("test",temp);
		if (CITY_SHANGHAI.equals(temp)) {
			flag = true;
		}
		Log.i("test",flag + "");
		return flag;
	}

	public boolean isBeiJing() {
		intSharedPreferences(mContext);
		String temp = prefs.getString(CURRENT_CITY, "--");
		Log.i("test","isBeiJing");
		Log.i("test",temp);
		boolean flag = false;
		if ("--".equals(temp)) {
			checkBeijing();
		}
		temp = prefs.getString(CURRENT_CITY, "--");
		Log.i("test",temp);
		if (CITY_BEIJING.equals(temp)) {
			flag = true;
		} else {
			flag = false;
		}
		Log.i("test",flag + "");
		return flag;
	}

	public boolean hasShow() {
		intSharedPreferences(mContext);
		String temp = prefs.getString(APPLICATION_FIRST, "--");
		Log.i("test","hasShow");
		Log.i("test",temp);
		boolean flag = false;
		if ("--".equals(temp)) {
			Editor ed = prefs.edit();
			ed.putString(APPLICATION_FIRST, "UN");
			ed.commit();
		}
		temp = prefs.getString(APPLICATION_FIRST, "--");
		Log.i("test",temp);
		if (APPLICATION_FIRST.equals(temp)) {
			flag = true;
		}
		Log.i("test",flag + "");
		return flag;
	}

	public void checkShangHai() {
		intSharedPreferences(mContext);
		Log.i("test","checkShangHai");
		Editor ed = prefs.edit();
		ed.putString(CURRENT_CITY, CITY_SHANGHAI);
		ed.commit();
	}

	public void checkBeijing() {
		intSharedPreferences(mContext);
		Log.i("test","checkBeijing");
		Editor ed = prefs.edit();
		ed.putString(CURRENT_CITY, CITY_BEIJING);
		ed.commit();
	}

	public void checkShow() {
		intSharedPreferences(mContext);
		Log.i("test","checkShow");
		Editor ed = prefs.edit();
		ed.putString(APPLICATION_FIRST, APPLICATION_FIRST);
		ed.commit();
	}

	public boolean isFirstPop() {
		intSharedPreferences(mContext);
		String temp = prefs.getString(MAPVIEW_FIRST, "--");
		Log.i("test","MAPVIEW_FIRST");
		Log.i("test",temp);
		boolean flag = true;
		if ("--".equals(temp)) {
			Editor ed = prefs.edit();
			ed.putString(MAPVIEW_FIRST, "UN");
			ed.commit();
		}
		temp = prefs.getString(MAPVIEW_FIRST, "--");
		Log.i("test",temp);
		if (MAPVIEW_FIRST.equals(temp)) {
			flag = false;
		}
		Log.i("test",flag + "");
		return flag;
	}

	public void setNotFirstFlag() {
		intSharedPreferences(mContext);
		Log.i("test","setNotFirstFlag");
		Editor ed = prefs.edit();
		ed.putString(MAPVIEW_FIRST, MAPVIEW_FIRST);
		ed.commit();

	}

	public void list() {
		intSharedPreferences(mContext);
		Map<String, ?>  map=prefs.getAll();
		Iterator iter = map.entrySet().iterator();
		while (iter.hasNext()) {
		Map.Entry<String,String>entry = (Map.Entry) iter.next();
		String key = entry.getKey();
		String val = entry.getValue();
		Log.i("test","key ="+key);
		Log.i("test","val ="+val);
		
		}
	}
}
